<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Config_Email extends CI_Controller {

	/* Layout utilizado pela pagina. */
	public $layout = 'default_admin';
	
	/* Titulo da Pagina. */
	public $title = CLIENTE;
	
	/* Menu do sistema*/
	public $menuTopo = '';
	
	/* Menu do sistema*/
	public $menuLateral = '';
	
	/* Caminho do pão do sistema */
	public $caminhoPao = '';
	
	/* css utilizado pela pagina. */
	public $css = array('admin/bootstrap', 'admin/animate', 'admin/style', 'plugins/waitMe/waitMe' );
	
	/* Js utilizado pela pagina. */
	public $js = array('plugins/jquery/jquery-2.1.4', 'admin/bootstrap', 'plugins/metisMenu/jquery.metisMenu', 'plugins/slimscroll/jquery.slimscroll.min', 'admin/principal', 'plugins/pace/pace.min', 'plugins/jquery-cookie/jquery.cookie', 'plugins/waitMe/waitMe');
	
	private $classe;
	
	public function __construct()
	{
		parent::__construct();
		//Verifica se o usuário está logado
		usuarioLogado($this->session->userdata('logged'));
		
		$this->load->model('Config_Email_Model', 'email');
		
		$this->classe = trim(strtolower(get_class($this)));	
		
		//Valida permissões do usuário
		$this->permissao_lib->validaUsuarioPermissaoAcessarModulo($this->classe);
		
		//Montando menu dinâmico
		$this->load->library('Menu_Lib', 'menu_lib');
		$this->menuTopo = $this->menu_lib->menuTopo();
		$this->menuLateral = $this->menu_lib->menuLateral($this->classe);
		$this->caminhoPao = $this->menu_lib->caminhoPao($this->classe, null, null);
	}
	
	public function index()	{					
		try {
				array_push($this->js, 'plugins/formValidation/formValidation', 'plugins/framework/bootstrap', 'admin/config_email/config_email');
				array_push($this->css, 'plugins/formValidation/formValidation');
				
				$configEmail = $this->email->obter();
				
				$this->load->view('admin/config_email/config_email', $configEmail);
			
		}catch (Exception $e){
			echo ($e->getMessage());
		}
		
	}
	
	public function salvar(){
		
		try {
				
			$this->layout = "";
			
			$this->util_lib->validaPermissaoRegistro("salvar", $this->classe);
			
			$retornoJson = false;
			$tipoMensagem = "";
			$mensagem = "";
			
			$alteracao = false;
				
			$protocol      = trim($this->input->post('protocol'));
			$smtpHost      = trim($this->input->post('smtpHost'));
			$smtpPort      = trim($this->input->post('smtpPort'));
			$smtpUser      = trim($this->input->post('smtpUser'));
			$smtpPass      = trim($this->input->post('smtpPass'));
			$smtpTimeOut   = trim($this->input->post('smtpTimeOut'));
			$mailType      = trim($this->input->post('mailType'));
			$charset       = trim($this->input->post('charset'));
			$fromEmail	   = trim($this->input->post('fromEmail'));
			$fromDescricao = trim($this->input->post('fromDescricao'));
				
			//Usuário Logado
			$idUsuario = decrypt(trim($this->session->userdata('idUsuario')));
				
			$configEmail = $this->email->obter();
				
			if (empty($configEmail)){
				$configEmail['protocol'] = $protocol;
				$configEmail['smtpHost'] = $smtpHost;
				$configEmail['smtpPort'] = $smtpPort;
				$configEmail['smtpUser'] = $smtpUser;
				$configEmail['smtpPass'] = $smtpPass;
				$configEmail['smtpTimeOut'] = $smtpTimeOut;
				$configEmail['mailType'] = $mailType;
				$configEmail['charset'] = $charset;
				$configEmail['fromEmail'] = $fromEmail;
				$configEmail['fromDescricao'] = $fromDescricao;
				$configEmail['idUsuarioUltimaAlteracao'] = $idUsuario;
				$configEmail['dtUltimaAlteracao'] = date("Y-m-d H:i:s");
			}else{
				$alteracao = true;
				$configEmail = objetoToArray($configEmail);
				$configEmail['protocol'] = $protocol;
				$configEmail['smtpHost'] = $smtpHost;
				$configEmail['smtpPort'] = $smtpPort;
				$configEmail['smtpUser'] = $smtpUser;
				$configEmail['smtpPass'] = $smtpPass;
				$configEmail['smtpTimeOut'] = $smtpTimeOut;
				$configEmail['mailType'] = $mailType;
				$configEmail['charset'] = $charset;
				$configEmail['fromEmail'] = $fromEmail;
				$configEmail['fromDescricao'] = $fromDescricao;
				$configEmail['idUsuarioUltimaAlteracao'] = $idUsuario;
				$configEmail['dtUltimaAlteracao'] = date("Y-m-d H:i:s");
			}
				
			//Iniciando transação
			$this->db->trans_begin();
				
			if ($alteracao){
			
				if ($this->email->alterar($configEmail)){
					$retornoJson = true;
					$tipoMensagem = "alert-success";
					$mensagem = "Registro alterado com sucesso!";
				}
			
			}else{
				if ($this->email->inserir($configEmail)){
					$retornoJson = true;
					$tipoMensagem = "alert-success";
					$mensagem = "Registro alterado com sucesso!";
				}
			}
				
			if ($retornoJson){
				$this->db->trans_commit();
			}else{
				$this->db->trans_rollback();
			}
				
			$json = null;
			$json['sucesso'] = $retornoJson;
			$json['tipoMensagem'] = $tipoMensagem;
			$json['mensagem'] = $mensagem;
				
			echo json_encode($json);
	
		} catch (Exception $e){
			$this->db->trans_rollback();
			
			$json['sucesso'] = false;
			$json['tipoMensagem'] = "alert-danger";
			$json['mensagem'] = $e->getMessage();
			
			echo json_encode($json);
		}
	}
}